package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.CustomerSaleScopeAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.convert.CustomerSaleScopeAdapterConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.request.CustomerSalesScopeQueryRequest;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.CustomerSaleScopeDTO;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.BeanConverter;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.domain.customersalescope.dto.BatchQueryByCustomerCodesRequest;
import com.epoch.app.crplatformenhance.domain.customersalescope.model.CustomerSaleScope;
import com.epoch.app.crplatformenhance.domain.customersalescope.service.CustomerSaleScopeService;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName CustomerSaleScopeAdapterImpl
 * @description: TODO
 * @date 2023/05/08 17:08
 * @version: 1.0
 */
@Service
public class CustomerSaleScopeAdapterImpl implements CustomerSaleScopeAdapter {

    @Resource
    private CustomerSaleScopeService customerSaleScopeService;

    @Override
    public List<CustomerSaleScopeDTO> batchQueryByCustomerCodes(CustomerSalesScopeQueryRequest queryRequest) {

        BatchQueryByCustomerCodesRequest request = BeanConverter.convert(queryRequest, new BatchQueryByCustomerCodesRequest());

        Result<List<CustomerSaleScope>> listResult = customerSaleScopeService.batchQueryByCustomerCodes(request);

        // 过滤 业务单元=1的客户送达方
        List<CustomerSaleScopeDTO> result = CustomerSaleScopeAdapterConvertor.convert(listResult)
                .stream()
                .filter(p -> !StringUtils.equals("1", p.getOrgBussinessUnitCode()))
                .collect(Collectors.toList());

        return result;
    }

    @Override
    public Map<String, CustomerSaleScopeDTO> batchQueryMapByCustomerCodes(CustomerSalesScopeQueryRequest queryRequest) {

        List<CustomerSaleScopeDTO> customerSaleScopeList = batchQueryByCustomerCodes(queryRequest);
        if(CollectionUtils.isEmpty(customerSaleScopeList)) {
            return Maps.newHashMap();
        }
        return customerSaleScopeList.stream()
                .collect(Collectors.toMap(CustomerSaleScopeDTO::getCustomerCode, p->p,(v1, v2)->v1));
    }
}
